<script setup lang='ts'>
import WorkTable from '../work/components/workTable.vue';
import { ref } from 'vue';
import Member from './components/member.vue';
import { useRoute } from 'vue-router';
import Avatar from '../../components/avatar.vue';
import RoomDetail from './components/roomDetail.vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { reqExitMemberProjectById, reqDeleteProjectById, reqGetProjectById } from '../../apis/project';
import useUserStore from '../../store/modules/user';
import { reqGetAllStoryByProjectId } from '../../apis/story/index';
import { onMounted } from 'vue';
import type { ProjectItem } from '../../apis/project/type'
import AddDialog from '../work/components/addDialog.vue'
import type { StoryItem } from '../../apis/story/type';

const projecRequirementList = ref<StoryItem[]>([])
const projectBugList = ref<StoryItem[]>([])

const addDialogVisible = ref<boolean>(false)
const useUser = useUserStore()
const activeTab = ref<string>('1')
let memberDrawerVisible = ref<boolean>(false)
let roomDetailVisible = ref<boolean>(false)
const $route = useRoute()
let currentProject = ref<ProjectItem>({} as ProjectItem)

const getProjectInfo = async () => {
  const res = await reqGetProjectById(Number($route.params.id))
  if (res.code === 200) {
    currentProject.value = res.data
  }
}

const handlerExitRoom = () => {
  ElMessageBox.confirm(`确认要退出空间 ${currentProject.value.name} 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(async () => {
    const res = await reqExitMemberProjectById(currentProject.value.id)
    if (res.code === 200) {
      useUser.aboutAsyncRoutes()
      ElMessage.success('退出成功')
    }
  })
}

const handlerDeleteRoom = () => {
  ElMessageBox.confirm(`此操作将永远删除空间 ${currentProject.value.name}, 是否继续?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(async () => {
    const res = await reqDeleteProjectById(Number(currentProject.value.id))
    if (res.code === 200) {
      useUser.aboutAsyncRoutes()
      ElMessage.success('删除成功')
    }
  })
}

const isCreater = (id: number) => {
  const project = useUser.projectData.find((item: any) => item.id === currentProject.value.id)
  return project?.createdBy?.id === id;
}

const getProjectAllRequirement = async () => {
  const res = await reqGetAllStoryByProjectId(Number($route.params.id), 'requirement')
  if (res.code === 200) {
    projecRequirementList.value = res.data
    console.log(res.data);
  }
}

const getProjectAllBug = async () => {
  const res = await reqGetAllStoryByProjectId(Number($route.params.id), 'bug')
  if (res.code === 200) {
    projectBugList.value = res.data
    console.log(res.data);
  }
}

const memberChangeRefresh = () => {
  getProjectAllRequirement()
  getProjectAllBug()
}


onMounted(() => {
  getProjectAllRequirement()
  getProjectAllBug()
  getProjectInfo()
})
</script>


<template>
  <el-card>
    <div class="cord-header">
      <el-menu :default-active="activeTab" class="el-menu-demo" mode="horizontal"
        @select="(index: string) => activeTab = index">
        <div class="title-container">
          <Avatar :name="`${$route.meta.title}`" :size="28"></Avatar>
          <span class="title-text" @click="roomDetailVisible = true">{{ currentProject.name }}</span>
          <el-button class="add-button" v-if="isCreater(useUser.id as number) || useUser.role === 'admin'"
            @click="addDialogVisible = true">添加待办事项</el-button>
        </div>
        <el-menu-item index="1">需求</el-menu-item>
        <el-menu-item index="2">缺陷</el-menu-item>
        <div class="but">
          <el-button icon="User" style="margin-right: 10px;" @click="memberDrawerVisible = true"></el-button>
          <el-dropdown>
            <el-button type="default" icon="More"></el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item icon="House" @click="roomDetailVisible = true">空间详情</el-dropdown-item>
                <el-dropdown-item v-if="isCreater(useUser.id as number) || useUser.role === 'admin'" divided
                  icon="SwitchButton" @click="handlerDeleteRoom">删除空间</el-dropdown-item>
                <el-dropdown-item v-else divided icon="SwitchButton" @click="handlerExitRoom">退出空间</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-menu>
    </div>
    <el-scrollbar height="100%">
      <div>
        <WorkTable v-if="activeTab === '1'" :workData="projecRequirementList" :height="'75vh'"
          @refresh="getProjectAllRequirement">
        </WorkTable>
        <WorkTable v-else :workData="projectBugList" :height="'75vh'" @refresh="getProjectAllBug"></WorkTable>
      </div>
    </el-scrollbar>
    <Member v-model:status="memberDrawerVisible" @refresh="memberChangeRefresh"></Member>
    <RoomDetail v-model:detailVisible="roomDetailVisible"></RoomDetail>
    <AddDialog v-model:dialogVisible="addDialogVisible" @refresh="() => {
      getProjectAllBug()
      getProjectAllRequirement()
    }">
    </AddDialog>
  </el-card>
</template>

<style scoped lang='scss'>
.cord-header {
  position: relative;

  .title-container {
    display: flex;
    align-items: center;
    margin-right: 30px;
    height: 60px;

    .title-text {
      font-weight: 600;
      font-size: 24px;
      margin: 0 15px 0 5px;

      &:hover {
        cursor: pointer;
        border-bottom: 1px solid rgb(64, 158, 255);
        color: rgb(64, 158, 255);
      }
    }

    .add-button {
      margin-top: 2px;
    }

  }

  .but {
    display: flex;
    align-items: center;
    margin-top: 2px;
    height: 60px;
    position: absolute;
    top: 0;
    right: 0;
  }
}


:deep(.el-menu--horizontal) {
  .el-menu-item {
    height: 60px;
    line-height: 60px;
  }
}
</style>